package com.atguigu.webapp.service.impl;

import com.atguigu.model.enums.ItemType;
import com.atguigu.model.pojo.*;

import com.atguigu.webapp.mapper.*;
import com.atguigu.webapp.service.LeaseAgreementService;
import com.atguigu.webapp.vo.agreement.AgreementDetailVo;
import com.atguigu.webapp.vo.agreement.AgreementItemVo;
import com.atguigu.webapp.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 梅超凡
 * @Date 2024/11/12~21:47
 * @Description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
*/
@Service
@Slf4j
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {


    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public List<AgreementItemVo> listAgreementItemByPhone(String phone) {
        List<AgreementItemVo> voList = leaseAgreementMapper.listAgreementItemByPhone(phone);
        log.info("查询结果为：{}",voList);
        return voList;

    }

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Override
    public AgreementDetailVo getAgreementDetailById(Long id) {
        // 1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(leaseAgreement.getApartmentId());
        // 3.查询公寓图片
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,leaseAgreement.getApartmentId());
        // 4.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectRoomById(leaseAgreement.getRoomId());
        // 5.查询房间图片
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM,leaseAgreement.getRoomId());
        // 6.查询租约期限
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        // 7.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        // 8.封装数据
        AgreementDetailVo detailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, detailVo);
        // 属性赋值
        // 1.公寓名称
        detailVo.setApartmentName(apartmentInfo.getName());
        // 2.公寓图片列表
        detailVo.setApartmentGraphVoList(apartmentGraphVoList);
        // 3.房间号
        detailVo.setRoomNumber(roomInfo.getRoomNumber());
        // 4.房间图片列表
        detailVo.setRoomGraphVoList(roomGraphVoList);
        // 5.支付方式
        detailVo.setPaymentTypeName(paymentType.getName());
        // 6.租期月数
        detailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        // 7.租期单位
        detailVo.setLeaseTermUnit(leaseTerm.getUnit());

        log.info("查询结果为：{}",detailVo);
        return detailVo;
    }
}




